<template>
  <div class="detail-container">
    <!-- 商品基本信息 -->
    <div class="product-info">
      <div class="product-image">
        <el-image 
          :src="product.image" 
          :preview-src-list="[product.image]"
          fit="cover"
        />
      </div>
      <div class="product-details">
        <h1>{{ product.name }}</h1>
        <div class="description" v-html="product.description"></div>
        <p class="price">¥{{ product.price }}</p>
        <p class="sales">销量：{{ product.sales }}</p>
        <div class="actions">
          <div class="quantity">
            <el-input-number 
              v-model="quantity" 
              :min="1" 
              size="small"
            />
          </div>
          <el-button type="warning" @click="addToCart">加入购物车</el-button>
          <el-button type="danger" @click="buyNow">立即购买</el-button>
        </div>
      </div>
    </div>

    <!-- 评论区 -->
    <div class="comments-section">
      <h2>用户评价</h2>
      <div class="comments-list" v-infinite-scroll="loadMoreComments">
        <div v-for="comment in comments" :key="comment.id" class="comment-item">
          <div class="comment-user">
            <el-avatar :src="comment.userAvatar" :size="50" />
            <span>{{ comment.userName }}</span>
          </div>
          <div class="comment-content">
            <p class="comment-text">{{ comment.content }}</p>
            <div class="comment-images" v-if="comment.images?.length">
              <el-image 
                v-for="(image, index) in comment.images" 
                :key="index" 
                :src="image"
                :preview-src-list="comment.images"
                fit="cover"
              />
            </div>
            <div class="comment-footer">
              <span class="comment-time">{{ formatDate(comment.createTime) }}</span>
              <el-button 
                type="text"
                @click="likeComment(comment)"
              >
                <i class="fas fa-thumbs-up" :class="{ active: comment.isLiked }"></i>
                {{ comment.likes }}
              </el-button>
            </div>
          </div>
        </div>
        <div v-if="loading" class="loading">
          <el-icon class="is-loading"><Loading /></el-icon>
          加载中...
        </div>
        <div v-if="noMore" class="no-more">没有更多评论</div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { Loading } from '@element-plus/icons-vue'

const props = defineProps({
  productId: {
    type: [Number, String],
    required: true
  }
})

const emit = defineEmits(['close', 'add-to-cart'])

const router = useRouter()
const activeTab = ref('detail')
const quantity = ref(1)
const loading = ref(false)
const noMore = ref(false)
const currentPage = ref(1)

const product = ref({
  id: props.productId,
  name: '测试商品',
  price: 199.99,
  sales: 100,
  image: 'https://via.placeholder.com/400',
  description: '<p>这是商品详情描述</p>'
})

const comments = ref([
  {
    id: 1,
    userAvatar: 'https://via.placeholder.com/40',
    userName: '用户1',
    content: '商品很好',
    images: ['https://via.placeholder.com/100'],
    createTime: new Date().toISOString(),
    likes: 10,
    isLiked: false
  }
])

// 加载更多数据
const loadMoreDetails = async () => {
  if (loading.value || noMore.value) return
  loading.value = true
  
  try {
    // TODO: API - 加载更多商品详情
    await new Promise(resolve => setTimeout(resolve, 1000))
    product.value.description += '<p>加载更多的商品详情...</p>'
    
    if (currentPage.value >= 3) {
      noMore.value = true
    } else {
      currentPage.value++
    }
  } catch (error) {
    ElMessage.error('加载失败')
  } finally {
    loading.value = false
  }
}

const loadMoreComments = async () => {
  if (loading.value || noMore.value) return
  loading.value = true
  
  try {
    // TODO: API - 加载更多评论
    await new Promise(resolve => setTimeout(resolve, 1000))
    
    const newComment = {
      id: comments.value.length + 1,
      userAvatar: 'https://via.placeholder.com/40',
      userName: `用户${comments.value.length + 1}`,
      content: '新加载的评论',
      images: [],
      createTime: new Date().toISOString(),
      likes: 0,
      isLiked: false
    }
    
    comments.value.push(newComment)
    
    if (currentPage.value >= 3) {
      noMore.value = true
    } else {
      currentPage.value++
    }
  } catch (error) {
    ElMessage.error('加载失败')
  } finally {
    loading.value = false
  }
}

// 其他方法
const addToCart = () => {
  emit('add-to-cart', props.productId, quantity.value)
}

const buyNow = () => {
  router.push({
    path: '/cart',
    query: {
      productId: props.productId,
      quantity: quantity.value,
      direct: true
    }
  })
}

const likeComment = (comment) => {
  comment.likes += comment.isLiked ? -1 : 1
  comment.isLiked = !comment.isLiked
}

const formatDate = (date) => {
  return new Date(date).toLocaleDateString()
}
</script>

<style scoped>
.detail-container {
  padding: 20px;
}

.product-info {
  display: flex;
  gap: 30px;
  margin-bottom: 30px;
}

.product-image {
  width: 300px;
  height: 300px;
}

.product-image :deep(.el-image) {
  width: 100%;
  height: 100%;
  border-radius: 8px;
}

.product-details {
  flex: 1;
}

.product-details h1 {
  margin: 0 0 10px;
  font-size: 24px;
}

.description {
  font-size: 14px;
  color: #666;
  margin-bottom: 15px;
  line-height: 1.5;
}

.price {
  font-size: 28px;
  color: #ff4444;
  font-weight: bold;
  margin: 15px 0;
}

.sales {
  color: #666;
  margin-bottom: 20px;
}

.actions {
  display: flex;
  gap: 15px;
  align-items: center;
}

.quantity {
  width: 120px;
}

.comments-section {
  background: white;
  border-radius: 8px;
  padding: 20px;
}

.comments-section h2 {
  margin: 0 0 20px;
  font-size: 20px;
}

.comments-list {
  /* 移除height和overflow-y属性 */
}

.comment-item {
  display: flex;
  gap: 20px;
  padding: 20px 0;
  border-bottom: 1px solid #eee;
}

.comment-user {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  width: 80px;
}

.comment-user span {
  font-size: 14px;
  color: #666;
}

.comment-content {
  flex: 1;
}

.comment-text {
  font-size: 16px;
  margin: 0 0 15px;
  line-height: 1.5;
}

.comment-images {
  display: flex;
  gap: 10px;
  margin: 15px 0;
}

.comment-images :deep(.el-image) {
  width: 100px;
  height: 100px;
  border-radius: 4px;
}

.comment-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 15px;
  color: #666;
}

.comment-time {
  font-size: 14px;
}

.loading,
.no-more {
  text-align: center;
  padding: 15px 0;
  color: #999;
  font-size: 14px;
}

.loading .el-icon {
  margin-right: 5px;
}

.comments-list::-webkit-scrollbar,
.comments-list::-webkit-scrollbar-thumb {
  display: none;
}

:deep(.el-button--text) {
  font-size: 16px;
}

:deep(.el-button--text i) {
  margin-right: 5px;
}

:deep(.el-button--text i.active) {
  color: #ff4444;
}
</style>